#include "Loader/obseFunctions.h"
#include "Loader/common.h"

#include "obse/PluginAPI.h"
#include "obse/GameAPI.h"
#include "obse/GameForms.h"
#include "obse/Script.h"

double runScriptNoArgs(UInt32 formID) {
	Script* script = OBLIVION_CAST(LookupFormByID(formID), TESForm, Script);
	OBSEArrayVarInterface::Element result;
	if ( script ) {
		g_scriptIntfc->CallFunction(script, NULL, NULL, &result, 0);
	}
	else
		_ERROR("Script &lt;%p&gt; not found.", formID);
	return result.Number();
}

TESLeveledList* LL = NULL;
const UInt32 id_LL0LootSoulGemsEmpty100 = 0x0003E4ED;
double getSoulLvFromLL(UInt32 lv) {
	UInt16 level = -1;
	if ( !LL )
		LL = OBLIVION_CAST(LookupFormByID(id_LL0LootSoulGemsEmpty100), TESForm, TESLeveledList);
	
	if ( LL ) {
		for ( TESLeveledList::ListEntry* i = &LL->list; i && i->data; i = i->next ) {
			TESSoulGem* sg = OBLIVION_CAST(i->data->form, TESForm, TESSoulGem);
			if ( sg && sg->capacity == lv && i->data->level < level)
				level = i->data->level;
		}
	}
	else {
		_ERROR("LL0LootSoulGemsEmpty100 <0003E4ED> not found.");
	}
	return level;
}

double addToSavegame(UInt32 formID) {
	TESForm* form = LookupFormByID(formID);
	if ( form ) {
		AddFormToDataHandler(*g_dataHandler, form);
		AddFormToCreatedBaseObjectsList(*g_createdBaseObjList, form);
		return 1;
	}
	return 0;
}
